package jp.sinya.xml_parse;

import org.w3c.dom.*;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.IOException;

public class JaxpParse {
    public static final String xmlPath = "javademo/src/jp/sinya/person.xml";

    public void parseAll() {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            //解析xml文件
            Document document = builder.parse(xmlPath);
            parse(document);
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    public void parse(Node node) {

        if (node.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(node.getNodeName());
        }

        NodeList childNodes = node.getChildNodes();

        for (int i = 0; i < childNodes.getLength(); i++) {
            Node item = childNodes.item(i);
            parse(item);
        }
    }

    public void parseTagNameAll(String labelName) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

        try {
            DocumentBuilder builder = factory.newDocumentBuilder();

            //解析xml文件
            Document document = builder.parse(xmlPath);

            //查询指定的名称的标签的value
            NodeList list = document.getElementsByTagName(labelName);
            for (int i = 0; i < list.getLength(); i++) {
                Node node = list.item(i);
                String content = node.getTextContent();
                System.out.println(content);
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        }
    }

    public void parseTagName(String labelName) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlPath);

            //查询指定的名称的标签的value
            NodeList list = document.getElementsByTagName(labelName);
            if (list != null && list.getLength() > 0) {
                Node node = list.item(0);
                String content = node.getTextContent();
                System.out.println(content);
            }
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void addTag(String labelName, String newLabelName, String value) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlPath);

            NodeList elements = document.getElementsByTagName(labelName);
            if (elements != null && elements.getLength() > 0) {
                Node node = elements.item(0);

                //创建要新添加的element
                Element element = document.createElement(newLabelName);
                Text textNode = document.createTextNode(value);
                element.appendChild(textNode);
                //实际上只是添加中内存，并没有对文件有变更
                node.appendChild(element);

                //回写到文件中
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                transformer.transform(new DOMSource(document), new StreamResult(xmlPath));
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    public void updateLabel(String labelName, String newValue) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlPath);

            NodeList elements = document.getElementsByTagName(labelName);
            if (elements != null && elements.getLength() > 0) {
                Node node = elements.item(0);
                node.setTextContent(newValue);
                //回写到文件中
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                transformer.transform(new DOMSource(document), new StreamResult(xmlPath));
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }

    public void deleteLabel(String labelName) {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        try {
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(xmlPath);

            NodeList elements = document.getElementsByTagName(labelName);
            if (elements != null && elements.getLength() > 0) {
                Node node = elements.item(0);
                Node parentNode = node.getParentNode();
                parentNode.removeChild(node);

                //回写到文件中
                TransformerFactory transformerFactory = TransformerFactory.newInstance();
                Transformer transformer = transformerFactory.newTransformer();
                transformer.transform(new DOMSource(document), new StreamResult(xmlPath));
            }

        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
    }
}
